import { login, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
// import { resetRouter } from '@/router'

// 状态
const state = {
  token: getToken() || null,
  userInfo: {} // 没有做持久化哟
}
// 修改状态
const mutations = {
  setToken(state, data) {
    state.token = data
    setToken(data)
  },
  removeToken(state) {
    state.token = null
    removeToken()
  },
  setUserInfo(state, data) { // 设置用户资料
    state.userInfo = data
  },
  removeUserInfo(state) { // 删除用户资料
    state.userInfo = {}
  }

}
// 执行异步
const actions = {
  async login(context, data) {
    const res = await login(data)
    context.commit('setToken', res.token)
    setToken(res.token)
  },
  async getInfo(context) {
    const res = await getInfo()
    console.log(res)
    context.commit('setUserInfo', res.userinfo)
  },
  logout(context) {
    context.commit('removeToken')
    context.commit('removeUserInfo')
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

